<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2016/9/20
 * Time: 21:01
 */

namespace Application\Index\Controller;


use J\Intent;
use J\MVC\Controller;

class IndexController extends Controller
{
    protected function optionGet()
    {
        $this->optionIndex();
    }

    protected function optionIndex()
    {
        $pathinfo = current_pathinfo();

        /** @noinspection Php[Cannot resolve path]Inspection */
        $url = '/index.php/index/index/show';
        $content = '这是一个展示页，页面内容在控制器方法中直接ECHO或通过意图返回<br />
        <pre>pathinfo='. $pathinfo . '</pre>
        也可以通过<a href="' . $url . '">渲染模板<a>来实现动态输出<br />
        后台管理页面是通过Vemin渲染的，Vemin自动将Json对象转换为html页面<br />
        ';
        echo $content;
    }

    protected function optionElementUIJson(Intent $intent)
    {
        $intent->ajax(true, null, [
            'view' => [
                'name' => 'FormView',
                'style' => [
                    'width' => '460px',
                    'padding' => '40px',
                ],
                'url' => '/api.php/index/index/json',
                'fields' => [
                    [
                        'index' => 'json',
                        'label' => 'Json',
                        'name' => 'Json',
                        'type' => 'textarea',
                    ],
                ],
            ]
        ]);
    }

    protected function optionApiJson(Intent $intent)
    {
        $str = $_REQUEST['Json'];
        $str = str_replace(array("\n", "\r", ' '), "", $str);
        $str = str_replace("'", '"', $str);
        $str = preg_replace('/,\s*([\]}])/m', '$1', $str);
        $str = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/', '$1"$3":', $str);
        //$this->debug($str);

        $json = json_decode($str, true);
        $intent->ajax(true, null, [
            'redirect' => [
                'url' => '/element.php/index/index/Simple',
                'value' => ['text' => var_export($json, true)],
            ],
        ]);
    }

    protected function optionElementUISimple(Intent $intent)
    {
        $intent->ajax(true, null, [
            'view' => [
                'name' => 'Simple',
                'message' => $_REQUEST['text'],
            ],
        ]);
    }

    /**
     * @throws \J\Exception
     */
    protected function optionShow(Intent $intent)
    {
        $intent->value = $this->getView($intent->action, ['title'=>'模板编译', 'hello'=>'模板参数']);
    }

    protected function export($v)
    {
        highlight_string(var_export($v,true));
    }
}